package utils;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.Domain;
import model.Measureunit;
import model.Resource;
import model.Response;
import model.Resticket;
import model.Techresource;
import model.Techticket;
import model.Threshresource;
import model.Ticket;
import model.User;
import model.Userlog;
import model.Usertype;
import dtos.DomainDTO;
import dtos.MeasureunitDTO;
import dtos.ResourceDTO;
import dtos.ResponseDTO;
import dtos.ResticketDTO;
import dtos.TechresourceDTO;
import dtos.TechticketDTO;
import dtos.ThreshresourceDTO;
import dtos.TicketDTO;
import dtos.UserDTO;
import dtos.UserlogDTO;
import dtos.UsertypeDTO;

/**
* Session Bean implementation class Conv
*/
@Stateless
@Local
public class Conv {

       @PersistenceContext
       EntityManager em;

       
       /**
       * Default constructor.
       */
       public Conv() {
             // TODO Auto-generated constructor stub
       }
       
       /**
       * Method used to convert from Response to ResponseDTO
       * @param of type Response
       * @return of type ResponseDTO
       * 
        */
       public ResponseDTO fromEntityResponse(Response response) {
             ResponseDTO rd = new ResponseDTO();
             rd.setDate(response.getDate());
             rd.setDescription(response.getDescription());
             rd.setId(response.getId());
             rd.setIdUser(response.getUser().getId());
             return rd;
       }
       
       /**
       * Method used to convert from Doamin to DomainDTO
       * @param of type Domain
       * @return of type DomainDTO
       */
       public DomainDTO fromEntityDomain(Domain domain) {
             DomainDTO dd = new DomainDTO();
             
             dd.setId(domain.getId());
             dd.setName(domain.getName());
             if (domain.getUsers() != null && !domain.getUsers().isEmpty())
                    for (User u : domain.getUsers()) {
                           dd.addUser(u.getId());
                    }
             if (domain.getTechresources() != null && !domain.getTechresources().isEmpty())
                    for (Techresource t : domain.getTechresources()) {
                           dd.addTechresource(t.getId());
                    }
             return dd;
             
             
       }
       
       /**
       * Method used to convert from Usertype to UsertypeDTO
       * @param of type Usertype
       * @return of type UsertypeDTO
       */
       public UsertypeDTO fromEntityUsertype(Usertype type) {
             UsertypeDTO ud = new UsertypeDTO();
             ud.setId(type.getId());
             ud.setName(type.getName());
             return ud;
       }
       
       /**
       * Method used to convert from User to UserDTO
       * @param of type User
       * @return of type UserDTO
       */
       public UserDTO fromEntityUser(User u) {
             UserDTO ud = new UserDTO();
             ud.setId(u.getId());
             if (u.getDomain() != null)
                    ud.setIdDomain(u.getDomain().getId());
             ud.setIsActive(u.getIsactive());
             ud.setEmail(u.getEmail());
             ud.setName(u.getName());
             ud.setPassword(u.getPassword());
             ud.setUsername(u.getUsername());
             if (u.getUsertype() != null)
                    ud.setIdUsertype(u.getUsertype().getId());
             ud.setStatus(u.getStatus());
             ud.setIp(u.getIp());
             if (u.getUserlogs() != null && !u.getUserlogs().isEmpty())
                    for (Userlog ul : u.getUserlogs())
                           ud.addUserlogs(ul.getId());
             if (u.getUser() != null && u.getUser().getId() != 0) {
                    ud.setIdUser(u.getUser().getId());
             }
             return ud;

       }
       
       /**
       * Method used to convert from Userlog to UserlogDTO
       * @param of type Userlog
       * @return of type UserlogDTO
       */
       public UserlogDTO fromEntityUserlog(Userlog u) {
             UserlogDTO userlog = new UserlogDTO();
             userlog.setId(u.getId());
             userlog.setEndtime(u.getEndtime());
             userlog.setStarttime(u.getStarttime());
             if (u.getUser() != null)
                    userlog.setIdUser(u.getUser().getId());
             return userlog;
       }
       
       /**
       * Method used to convert from Techresource to TechresourceDTO
       * @param of type Techresource
       * @return of type TechresourceDTO
       */
       public TechresourceDTO fromEntityTechresource(Techresource t) {
             TechresourceDTO td = new TechresourceDTO();
             td.setId(t.getId());
             if (t.getDomain() != null)
                    td.setIdDomain(t.getDomain().getId());
             td.setIp(t.getIp());
             td.setIdnumber(t.getIdnumber());
             td.setName(t.getName());
             td.setStatus(t.getStatus());
             if (t.getTechtickets() != null && !t.getTechtickets().isEmpty())
                    for (Techticket tt : t.getTechtickets()) {
                           td.addTechticket(tt.getId());
                    }
             if (t.getThreshresources() != null && !t.getThreshresources().isEmpty())
                    for (Threshresource ts : t.getThreshresources()) {
                           td.addThreshresource(ts.getId());
                    }
             return td;
       }
       
       /**
       * Method used to convert from Resource to ResourceDTO
       * @param of type Resource
       * @return of type ResourceDTO
       */
       public ResourceDTO fromEntityResource(Resource r) {
             ResourceDTO rd = new ResourceDTO();
             rd.setId(r.getId());
             rd.setName(r.getName());
             if (r.getRestickets() != null && !r.getRestickets().isEmpty())
                    for (Resticket t : r.getRestickets()) {
                           rd.addResticket(t.getId());
                    }
             return rd;
       }
       
       /**
       * Method used to convert from Ticket to TicketDTO
       * @param of type Ticket
       * @return of type TicketDTO
       */
       public TicketDTO fromEntityTicket(Ticket t) {
             TicketDTO td = new TicketDTO();
             td.setDate(t.getDate());
             td.setId(t.getId());
             td.setDescription(t.getDescription());
             td.setStatus(t.getStatus());
             if (t.getResponse() != null)
                    td.setIdResponse(t.getResponse().getId());
             if (t.getUser() != null)
                    td.setIdUser(t.getUser().getId());
             return td;
       }
       
       /**
       * Method used to convert from Techticket to TechticketDTO
       * @param of type Techticket
       * @return of type TechticketDTO
       */
       public TechticketDTO fromEntityTechticket(Techticket t) {
             TechticketDTO td = new TechticketDTO();
             td.setId(t.getId());
             td.setDate(t.getDate());
             td.setDescription(t.getDescription());
             if (t.getResponse() != null)
                    td.setIdResponse(t.getResponse().getId());
             td.setStatus(t.getStatus());

             if (t.getUser() != null)
                    td.setIdUser(t.getUser().getId());

             if (t.getProblemtype() != null)
                    td.setProblemtype(t.getProblemtype());

             if (t.getTechresource() != null)
                    td.setIdTechresource(t.getTechresource().getId());
             return td;
       }
       
       /**
       * Method used to convert from Resticket to ResticketDTO
       * @param of type Resticket
       * @return of type ResticketDTO
       */
       public ResticketDTO fromEntityResticket(Resticket t) {
             ResticketDTO rd = new ResticketDTO();
             rd.setId(t.getId());
             rd.setDescription(t.getDescription());
             rd.setDate(t.getDate());
             rd.setStatus(t.getStatus());
             if (t.getUser() != null)
                    rd.setIdUser(t.getUser().getId());
             if (t.getMeasureunit() != null)
                    rd.setItMeasureunit(t.getMeasureunit().getId());
             if (t.getResponse() != null)
                    rd.setIdResponse(t.getResponse().getId());
             rd.setQuantity(t.getQuantity());
             if (t.getResource() != null)
                    rd.setIdResource(t.getResource().getId());
             return rd;
       }
       
       /**
       * Method used to convert from Measureunit to MeasureunitDTO
       * @param of type Measureunit
       * @return of type MeasureunitDTO
       */
       public MeasureunitDTO fromEntityMeasureunit(Measureunit u) {
             MeasureunitDTO md = new MeasureunitDTO();
             md.setId(u.getId());
             md.setName(u.getName());
             for (Resticket t : u.getRestickets()) {
                    md.addResticket(t.getId());
             }
             return md;
       }
       
       /**
       * Method used to convert from Threshresource to ThreshresourceDTO
       * @param of type Threshresource
       * @return of type ThreshresourceDTO
       */
       public ThreshresourceDTO fromEntityThreshresource(Threshresource t) {
             ThreshresourceDTO tr = new ThreshresourceDTO();
             tr.setName(t.getName());
             tr.setValue(t.getValue());
             if (t.getTechresources() != null && !t.getTechresources().isEmpty()) {
                    for (Techresource ti : t.getTechresources()) {
                           tr.addTechresource(ti.getId());
                    }
             }
             return tr;
       }
       
       /**
       * Method used to convert from DomainDTO to Domain
       * @param of type DomainDTO
       * @return of type Domain
       */
       public Domain fromEntityDomainDTO(DomainDTO d) {
             Domain domain = new Domain();
             domain.setName(d.getName());
             domain.setId(d.getId());
             if (d.getUsers() != null)
                    for (Integer i : d.getUsers()) {
                           domain.addUser(em.find(User.class, i));
                    }
             if (d.getTechresources() != null && !d.getTechresources().isEmpty())
                    for (Integer i : d.getTechresources()) {
                           domain.addTechresource(em.find(Techresource.class, i));
                    }
             return domain;
       }

       /**
       * Method used to convert from MeasureunitDTO to Measureunit
       * @param of type MeasureunitDTO
       * @return of type Measureunit
       */
       public Measureunit fromEntityMeasureunitDTO(MeasureunitDTO m) {
             Measureunit measure = new Measureunit();
             measure.setName(m.getName());
             measure.setId(m.getId());
             if (m.getRestickets() != null && !m.getRestickets().isEmpty())
                    for (Integer i : m.getRestickets()) {
                           measure.addResticket(em.find(Resticket.class, i));
                    }
             return measure;
       }

       /**
       * Method used to convert from ResourceDTO to Resource
       * @param of type ResourceDTO
       * @return of type Resource
       */
       public Resource fromEntityResourceDTO(ResourceDTO d) {
             Resource res = new Resource();
             res.setId(d.getId());
             res.setName(d.getName());
             if (d.getRestickets() != null && !d.getRestickets().isEmpty())
                    for (Integer i : d.getRestickets()) {
                           res.addResticket(em.find(Resticket.class, i));
                    }
             return res;
       }

       /**
       * Method used to convert from ResponseDTO to Response
       * @param of type ResponseDTO
       * @return of type Response
       */
       public Response fromEntityResponseDTO(ResponseDTO r) {
             Response resp = new Response();
             resp.setId(r.getId());
             resp.setDate(r.getDate());
             resp.setUser(em.find(User.class, r.getIdUser()));
             resp.setDescription(r.getDescription());
             return resp;

       }

       /**
       * Method used to convert from ResticketDTO to Resticket
       * @param of type ResticketDTO
       * @return of type Resticket
       */
       public Resticket fromEntityResticketDTO(ResticketDTO r) {
             Resticket res = new Resticket();
             res.setUser(em.find(User.class, r.getIdUser()));
             res.setId(r.getIdResticket());
             res.setStatus(r.getStatus());
             res.setDate(r.getDate());
             res.setDescription(r.getDescription());
             res.setQuantity(r.getQuantity());
             res.setResource(em.find(Resource.class, r.getIdResource()));
             res.setMeasureunit(em.find(Measureunit.class, r.getItMeasureunit()));
             res.setTicket(em.find(Ticket.class, r.getIdTicket()));
             return res;
       }

       /**
       * Method used to convert from TechresourceDTO to Techresource
       * @param of type TechresourceDTO
       * @return of type Techresource
       */
       public Techresource fromEntityTechresourceDTO(TechresourceDTO t) {
             Techresource tech = new Techresource();
             tech.setId(t.getId());
             tech.setName(t.getName());
             tech.setIp(t.getIp());
             tech.setStatus(t.getStatus());
             tech.setIdnumber(t.getIdnumber());
             if (tech.getDomain() != null)
                    tech.setDomain(em.find(Domain.class, t.getIdDomain()));
             if (t.getTechtickets() != null && !t.getTechtickets().isEmpty())
                    for (Integer i : t.getTechtickets()) {
                           tech.addTechticket(em.find(Techticket.class, i));
                    }
             if (t.getThreshresources() != null && !t.getThreshresources().isEmpty())
                    for (Integer i : t.getThreshresources()) {
                           tech.addThreshresource(em.find(Threshresource.class, i));
                    }

             return tech;
       }

       /**
       * Method used to convert from TechticketDTO to Techticket
       * @param of type TechticketDTO
       * @return of type Techticket
       */
       public Techticket fromEntityTechticketDTO(TechticketDTO r) {
             Techticket tick = new Techticket();
             tick.setId(r.getIdTicket());
             tick.setUser(em.find(User.class, r.getIdUser()));
             tick.setProblemtype(r.getProblemtype());
             tick.setStatus(r.getStatus());
             tick.setDate(r.getDate());
             tick.setDescription(r.getDescription());
             tick.setTicket(em.find(Ticket.class, r.getIdTechticket()));

             tick.setTechresource(em.find(Techresource.class, r.getIdTechresource()));
             return tick;
       }

       /**
       * Method used to convert from ThreshresourceDTO to Threshresource
       * @param of type ThreshresourceDTO
       * @return of type Threshresource
       */
       public Threshresource fromEntityThreshresourceDTO(ThreshresourceDTO t) {
             Threshresource tr = new Threshresource();
             tr.setId(t.getId());
             if (t.getTechresources() != null && !t.getTechresources().isEmpty())
                    for (Integer i : t.getTechresources()) {
                           tr.addTechresource(em.find(Techresource.class, i));
                    }
             tr.setName(t.getName());
             tr.setValue(t.getValue());
             return tr;
       }

       /**
       * Method used to convert from UserDTO to User
       * @param of type UserDTO
       * @return of type User
       */
       public User fromEntityUserDTO(UserDTO u) {
             User user = new User();
             user.setId(u.getId());
             user.setName(u.getName());
             user.setUsername(u.getUsername());
             user.setPassword(u.getPassword());
              user.setEmail(u.getEmail());
             user.setIsactive(u.getIsActive());
             user.setUser(em.find(User.class, u.getIdUser()));
             user.setDomain(em.find(Domain.class, u.getIdDomain()));
             user.setUsertype(em.find(Usertype.class, u.getIdUsertype()));
             user.setStatus(u.getStatus());
             user.setIp(u.getIp());
             if (u.getUserlogs() != null)
                    for (Integer i : u.getUserlogs()) {
                           user.addUserlog(em.find(Userlog.class, i));
                    }
             return user;

       }

       /**
       * Method used to convert from TicketDTO to Ticket
       * @param of type TicketDTO
       * @return of type Ticket
       */
       public Ticket fromEntityTicketDTO(TicketDTO t) {
             Ticket ticket = new Ticket();
             ticket.setId(t.getId());
             ticket.setStatus(t.getStatus());
             ticket.setDescription(t.getDescription());
             ticket.setDate(t.getDate());
             ticket.setResponse(em.find(Response.class, t.getIdResponse()));
             ticket.setResticket(em.find(Resticket.class, t.getIdResticket()));
             ticket.setTechticket(em.find(Techticket.class, t.getIdTechticket()));
             ticket.setUser(em.find(User.class, t.getIdUser()));
             return ticket;
       }

       /**
       * Method used to convert from UserlogDTO to Userlog
       * @param of type UserlogDTO
       * @return of type Userlog
       */
       public Userlog fromEntityUserlogDTO(UserlogDTO u) {
             Userlog userlog = new Userlog();
             userlog.setId(u.getId());
             userlog.setDate(u.getDate());
             userlog.setEndtime(u.getEndtime());
             userlog.setStarttime(u.getStarttime());

             userlog.setUser(em.find(User.class, u.getIdUser()));
             return userlog;
       }

       /**
       * Method used to convert from UsertypeDTO to Usertype
       * @param of type UsertypeDTO
       * @return of type Usertype
       */
       public Usertype fromEntityUsertypeDTO(UsertypeDTO u) {
             Usertype ut = new Usertype();
             ut.setId(u.getId());
             ut.setName(u.getName());
             if (u.getUsers() != null && !u.getUsers().isEmpty())
                    for (Integer i : u.getUsers()) {
                           ut.addUser(em.find(User.class, i));
                    }

             return ut;
       }

}

